package com.appspot.etao.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.appspot.etao.service.ClientFactory;
import com.taobao.top.TopJsonRestClient;
import com.taobao.top.domain.ResponseList;
import com.taobao.top.domain.ShopCat;
import com.taobao.top.request.ShopCatsGetRequest;

public class BaseServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		super.doGet(req, resp);
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(req, resp);
		setShopCats(req, resp);
	}

	//取得所有店铺所属类别放入session
	protected void setShopCats(HttpServletRequest req, HttpServletResponse resp) {
		TopJsonRestClient client = ClientFactory.getJsonClient();
		ResponseList<ShopCat> responseShopCatList = client.getShopCats(new ShopCatsGetRequest());
		List<ShopCat> shopCatList = responseShopCatList.getContent();
		
		Map<Long, String> shopCatMap = new HashMap<Long, String>();
		
		for (ShopCat shopCat : shopCatList) {
			shopCatMap.put(shopCat.getCid(), shopCat.getName());
		}
		
		req.getSession().setAttribute("shopCatMap", shopCatMap);
	}
}
